//////////////////////////////////////////////////////////////////
// Includes
//
#ifndef _THE_WEBCAM_H_
#define _THE_WEBCAM_H_

#include <sstream>
#include <algorithm>
#include "cglx.h"
#include "texture.h"

#define WEBCAM_IMAGES_ROOT_FOLDER "webcam_images/"

//////////////////////////////////////////////////////////////////
// Class Declaration
//
class WebCam
{
private:
    string myName;
    GLuint myMaxCount;
    GLuint myFrameCount;
    Texture myCurrentFrame;
    Texture myBackground;

public:
    /*
     * Initialize general OpenGL values once (in place of constructor).
     *
     * This includes, for example, light and texture parameters.
     */
    WebCam (const string& name, GLint maxFrame)
      : myName(name),
        myMaxCount(maxFrame),
        myFrameCount(0),
        myBackground(WEBCAM_IMAGES_ROOT_FOLDER + name + "/background.jpg")
    {}


    /*
     * Set camera's view of scene.
     */
    const Texture& nextFrame ()
    {
        // get next image
        ostringstream filename;
        filename << WEBCAM_IMAGES_ROOT_FOLDER << myName << "/" << myFrameCount << ".jpg";
        myCurrentFrame.updateData(filename.str());
        myFrameCount = (myFrameCount + 1) % myMaxCount;
        return myCurrentFrame;
    }

    /*
     * Get camera's current image.
     */
    const Texture& getCurrentFrame () const
    {
        return myCurrentFrame;
    }

    /*
     * Get camera's background image.
     */
    const Texture& getBackground () const
    {
        return myBackground;
    }

};

#endif
